package com.dingding.transfer.stamper.constant;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.dingding.transfer.stamper.entity.ProcessDetail;
import com.dingding.transfer.stamper.enums.ApplyDataSource;
import org.apache.commons.lang3.StringUtils;

/**
 * @author wangxiyuan
 * @description 审批常量
 * @createDate 2021/3/30 14:32
 **/
public interface ApprovalPara {
    String TASK_START = "start";
    String TASK_FINISH = "finish";
    String RESULT_AGREE  = "agree";
    String RESULT_REFUSE = "refuse";
    String TASK_TERMINATE = "terminate";
    String STAMPER_INFO = "用印";
    String FINGER_INFO  = "指纹";
    String PUSH_RECORD  = "推送申请单";
    String STAMPER_USING_TIME = "使用次数";
    String STAMPER_TYPE = "加盖印章";

    static int checkTaskStatus(JSONObject jsonObject){
        String type = jsonObject.getString("type");
        if(ApprovalPara.TASK_FINISH.equals(type)){
            String eventType = jsonObject.getString("EventType");
            if(ApprovalPara.RESULT_AGREE.equals(jsonObject.get("result"))
                    && EventType.APPROVE_INSTANCE.getEventType().equals(eventType)){
                return ApprovalEnum.APPLY_AGREE.getTaskStatus();
            }else if(ApprovalPara.RESULT_AGREE.equals(jsonObject.get("result"))
                    && EventType.APPROVE_INSTANCE.getEventType().equals(eventType)){
                return ApprovalEnum.APPLY_REJECT.getTaskStatus();
            }
            return ApprovalEnum.APPLY_PROCESS.getTaskStatus();
        }else if(ApprovalPara.TASK_START.equals(type)){
            return ApprovalEnum.APPLY_START.getTaskStatus();
        }else if(ApprovalPara.TASK_TERMINATE.equals(type)){
            return ApprovalEnum.APPLY_CANCEL.getTaskStatus();
        }
        return -1;
    }

    /*static ProcessRecord buildRecord(JSONObject jsonObject){
        String taskResult = jsonObject.getString("result");
        String title = jsonObject.getString("title");
        String processInstanceId = jsonObject.getString("processInstanceId");
        String processTime = StringUtils.isBlank(jsonObject.getString("finishTime")) ?
                jsonObject.getString("createTime") : jsonObject.getString("finishTime");
        // 审批模板的唯一码
        String processCode = jsonObject.getString("processCode");
        // 获取员工ID
        String staffId = jsonObject.getString("staffId");
        ProcessRecord record = ProcessRecord.builder().userid(staffId).processId(processInstanceId)
                .processTime(DateUtil.date(Long.parseLong(processTime)))
                .remark(jsonObject.getString("remark"))
                .result(taskResult)
                .build();
        return record;
    }*/

    static ProcessDetail buildDetail(JSONObject jsonObject){
        String processTime = StringUtils.isBlank(jsonObject.getString("finishTime")) ?
                jsonObject.getString("createTime") : jsonObject.getString("finishTime");
        ProcessDetail processDetail = ProcessDetail.builder().userid(jsonObject.getString("staffId"))
                .processId(jsonObject.getString("processInstanceId"))
                .processCode(jsonObject.getString("processCode"))
                .title(jsonObject.getString("title"))
                .createTime(DateUtil.date(Long.parseLong(processTime)))
                .source(ApplyDataSource.DING_DING)
                .build();
        return processDetail;
    }

    enum ApprovalEnum{
        APPLY_START(0),
        APPLY_PROCESS(1),
        APPLY_AGREE(2),
        APPLY_REJECT(3),
        APPLY_CANCEL(4);

        int taskStatus;

        ApprovalEnum(int taskStatus){

            this.taskStatus = taskStatus;
        }

        public int getTaskStatus() {
            return taskStatus;
        }

        public void setTaskStatus(int taskStatus) {
            this.taskStatus = taskStatus;
        }
    }
}
